package com.budweiser.user.service.impl;

import com.budweiser.model.core.constants.BaccoCoreMq;
import com.budweiser.model.organ.Organization;
import com.budweiser.user.dao.OrganizationDao;
import com.budweiser.user.dto.OrganSelectTree;
import com.budweiser.user.dto.OrganizationTree;
import com.budweiser.user.service.OrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OrganizationServiceImpl implements OrganizationService {
	@Autowired
	private OrganizationDao organizationDao;
	@Autowired
	private AmqpTemplate amqpTemplate;

	@Override
	public List<OrganizationTree> getOrganizationByAll() {
		log.info("构建机构管理首页树形表格{}");
		return organizationDao.getOrgainByAll();
	}

	@Override
	public List<OrganSelectTree> getOrganSelectTrees() {
		log.info("构建机构管理下拉树形{}");
		return organizationDao.getOrganSelectTree();
	}

	@Override
	public void save(Organization organization) {
		log.info("构建机构管理下拉树形{}", organization);
		organization.setCreateTime(new Date());
		organization.setDeleteStatus(false);
		organization.setUpdateTime(new Date());
		organization.setCode(getMaxCode());
		if (organization.getParentid() != null && organization.getParentid() != 0) {
			Organization parent = organizationDao.getOrganizationByPid(organization.getParentid());
			organization.setCode(parent.getCode() + getMaxCode());

		}
		organizationDao.insert(organization);
		amqpTemplate.convertAndSend(BaccoCoreMq.MQ_EXCHANGE_CORE, BaccoCoreMq.ROUTING_KEY_ACT_SAVE,
				"ORG," + organization.getId());
	}

	private void setChidCode(String parentCode, List<Organization> chids) {
		if (!CollectionUtils.isEmpty(chids)) {

			chids.forEach(c -> {
				c.setCode(parentCode + c.getId());
				organizationDao.update(c);
				List<Organization> chilList = organizationDao.getOrganizationChild(c.getId());
				setChidCode(c.getCode(), chilList);
			});
		}
	}

	public String getMaxCode() {
		String code = organizationDao.getMaxId();
		if (StringUtils.isNotEmpty(code)) {
			return code;
		}
		return "1";
	}

	@Override
	public void deleteIds(List<Long> ids) {
		log.info("批量删除机构{}", ids);
		organizationDao.deleteIds(ids);
		amqpTemplate.convertAndSend(BaccoCoreMq.MQ_EXCHANGE_CORE, BaccoCoreMq.ROUTING_KEY_ACT_ORG_DELETE, ids);
	}

	@Override
	public void update(Organization organization) {
		log.info("修改机构{}", organization);
		organization.setUpdateTime(new Date());
		if (organization.getParentid() != null && organization.getParentid() != 0) {
			Organization parent = organizationDao.getOrganizationByPid(organization.getParentid());
			organization.setCode(parent.getCode() + organization.getId());
		}
		updateChild(organization.getCode(), organization.getId());
		organizationDao.update(organization);
		amqpTemplate.convertAndSend(BaccoCoreMq.MQ_EXCHANGE_CORE, BaccoCoreMq.ROUTING_KEY_ACT_UPDATE,
				"ORG," + organization.getId());

	}

	private void updateChild(String parentCode, Long id) {
		Organization organization = organizationDao.getOrganizationByid(id);
		List<Organization> chids = organizationDao.getOrganizationChild(organization.getId());
		setChidCode(parentCode, chids);

	}

	@Override
	public Organization findOrganizationById(Long id) {
		return organizationDao.getOrganizationByid(id);
	}

}